<!DOCTYPE html>
<html lang="en">

<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>Document</title>
    <script src="https://unpkg.com/vue@3/dist/vue.global.js"></script>
    <script src="https://unpkg.com/mitt/dist/mitt.umd.js"></script>
    <!-- 用事件总线事项兄弟之间的通信，将form表单上的数据通过eventbus传递到table上实现兄弟之间的通信 -->
    <style>
        /* 王悦209000154 */
        li {
            list-style: none;
        }
        
        .center {
            margin: 0 auto;
            width: 400px;
            text-align: center;
        }
        
        .red {
            color: red;
        }
        
        .green {
            color: green;
        }
    </style>
</head>

<body>
    <div id="app"></div>
    <template id="root">
    <div class="center">
  
      <my-form></my-form>
      <my-table></my-table>
     </div>
     
    </div>
  </template>

    <template id="form">
    <div>
      <h3>用户注册</h3>
      <ul>
        <li>
          <label>用户名:</label><input type="text" @blur="checkUserName" v-model="username" placeholder="请输入用户名">
          <span :class="[this.errors.username=='校验通过'?'green':'red']">{{errors.username}}</span>
        </li>
        <li>
          <label>密码:</label><input type="password" @blur="checkPass" v-model=" password" placeholder="请输入密码">
          <span>{{errors.password}}</span>
        </li>
        <li>
          <label>性别:</label>
          <input type="radio" name="gender" value="male" v-model="gender" />男
          <input type="radio" name="gender" value="female" v-model="gender" />女
        </li>
        <li>
          <label>地址:</label>
          <select v-model="selectedCity">
            <option :value="c.name" v-for="c in cities">{{c.text}}</option>
          </select>
        </li>
      </ul>
      <button @click="handleRegister">注册</button>
    </div>
  </template>

    <template id="table">
    <div>
      <div style="display: flex;flex-direction: row;justify-content: center;">
        <table border="1">
          <tr>
            <td>序号</td>
            <td>用户名</td>
            <td>性别</td>
            <td>地址</td>
          </tr>
          <tr v-for="(u,index) in users">
            <td>{{index+1}}</td>
            <td>{{u.username}}</td>
            <td>{{u.gender=='male'?'男':'女'}}</td>
            <td>{{handleCity(u.city)}}</td>
          </tr>
        </table>
      </div>
    </div>
  </template>
</body>

<script>
    const emitter = window.mitt()
    const app = Vue.createApp({
        template: "#root",
    })

    app.component('MyForm', {
        template: '#form',
        data() {
            return {
                username: '',
                password: '',
                gender: 'male',
                cities: [{
                    name: 'cq',
                    text: '重庆'
                }, {
                    name: 'bj',
                    text: '北京'
                }, ],
                selectedCity: 'cq',
                errors: {},
                users: [],
                id: ''
            }
        },
        methods: {
            handleModify() {
                const user = this.users.find((u) => {
                    return u.id == this.id
                })
                user.username = this.username
                user.password = this.password
                user.gender = this.gender
                user.city = this.selectedCity
            },
            handleRegister() {
                if (this.errors.username == '校验通过' && this.errors.password == '校验通过') {

                    this.users.push({
                        username: this.username,
                        password: this.password,
                        gender: this.gender,
                        city: this.selectedCity
                    })
                    console.log(this.users)
                    emitter.emit('userlist', this.users)

                }
            },
            checkUserName() {
                if (this.username == "" || this.username == null) {
                    this.errors.username = "用户名不能为空"
                } else {
                    this.errors.username = "校验通过"
                }
            },
            checkPass() {
                if (this.password == "" || this.password == null) {
                    this.errors.password = "密码不能为空"
                } else {
                    if (this.password.length < 6) {
                        this.errors.password = "密码长度不正确"

                    } else {
                        this.errors.password = "校验通过"

                    }
                }
            },

        }

    })
    app.component('MyTable', {
        template: '#table',
        data() {
            return {
                users: [],
                cities: [{
                    name: 'cq',
                    text: '重庆'
                }, {
                    name: 'bj',
                    text: '北京'
                }, ],
            }
        },
        methods: {
            handleCity(name) {
                let text = ''
                this.cities.forEach(c => {
                    if (c.name == name) {
                        text = c.text
                    }
                });
                return text
            }
        },
        mounted() {
            emitter.on('userlist', res => {

                this.users = res
            })

        }
    })

    app.mount("#app")
</script>

</html>
